package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.DynamicProblem;

/**
 * 300. Longest Increasing Subsequence
 * 经典问题LIS
 * DP解法
 *
 * <p>
 * see LC354 俄罗斯套娃信封
 * LC56 区间合并
 *
 * @author tzp
 * @since 2020/9/16
 */
public class LC300 implements DynamicProblem {
    public int lengthOfLIS(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int[] state = new int[nums.length];
        int totalMax = Integer.MIN_VALUE;
        for (int i = nums.length - 1; i >= 0; i--) {
            if (i == nums.length - 1) state[i] = 1;
            int max = 1;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] < nums[j]) {
                    max = Math.max(max, state[j] + 1);
                }
            }
            state[i] = max;
            totalMax = Math.max(totalMax, max);
        }
        return totalMax;
    }

    public static void main(String[] args) {
        int[] a = {10, 9, 2, 5, 3, 7, 101, 18};
        int[] b = {-2, -1};
        int[] c = {2, 2};
        System.out.println(new LC300().lengthOfLIS(a));
        System.out.println(new LC300().lengthOfLIS(b));
        System.out.println(new LC300().lengthOfLIS(c));
    }
}
